<?php
class Wall_Form_Share extends Engine_Form
{
  public function init()
  {
    $translate = Zend_Registry::get('Zend_Translate');
    $this
      ->setTitle($translate->translate('Share'))
      ->setDescription($translate->translate('Share this by re-posting it with your own message.'))
      ->setMethod('POST')
      ->setAction(Zend_Controller_Front::getInstance()->getRouter()->assemble(array()))
      ;
    
    $this->addElement('Select', 'shareto', array(
      'label' => '',
      'multiOptions' => array(
        'me'  => $translate->translate('On your own wall'),
        'friend'  => $translate->translate("On your friends' walls"),
      ),
      'onchange' => 'javascript:changeShareType(this.value);'
    ));

    //Get custom lists
    $viewer = Engine_Api::_()->user()->getViewer();
    $lists_table = Engine_Api::_()->getDbtable('lists', 'wall');
    $items_table = Engine_Api::_()->getDbtable('listItems', 'wall');

    $select = $lists_table->select()->where('user_id = ?', $viewer->getIdentity());
    $custom_lists = $lists_table->fetchAll($select);
    
    $i = 2; $temp = '';
    foreach ($custom_lists as $list){
        $i++;
        $temp = $temp . '<tr><td><span id="pri'.$i.'" class="check" /></td><td id="txt_pri'.$i.'" listid="'.$list->list_id.'" onclick="setPrivacy('.$i.')">'.$translate->translate($list->list_name).'</td></tr>';
    }
    
    $this->addElement('Dummy', 'wall', array(
        'content' => '
            <div id="wall_privacy">
                  <a id="lock" href="javascript:void(0);">
                      <img title="Share with" id="lock_img" alt="share with" src="./application/modules/Wall/externals/images/l1.png" border="0" />
                  </a>
                  <table id="privacy" cellspacing="0" cellpadding="0" border="0">
                      <tr><td width="20"><span id="pri0" class="check" /></td><td width="140" id="txt_pri0" onclick="setPrivacy(0);">'.$translate->translate('Everyone').'</td></tr>
                      <tr><td><span id="pri1" class="check" /></td><td id="txt_pri1" onclick="setPrivacy(1)">'.$translate->translate('Friends of Friends').'</td></tr>
                      <tr><td><span id="pri2" class="check" /></td><td id="txt_pri2" onclick="setPrivacy(2)">'.$translate->translate('Friends Only').'</td></tr>'
                      .$temp.
                  '</table>
            </div>
        '
    ));
    
    // init to
    $this->addElement('Text', 'to',array(
        'label'=>$translate->translate('Type your friend name'),
        'order' => 2,
        'attribs' => array('placeholder'=>$translate->translate('Type your friend name ...')),
        'autocomplete'=>'off'));

    Engine_Form::addDefaultDecorators($this->to);
    
        // Init to Values
    $this->addElement('Hidden', 'toValues', array(
      'label' => 'Selected Friends',
      'required' => true,
      'allowEmpty' => false,
      'order' => 3,
      'validators' => array(
        'NotEmpty'
      ),
      'filters' => array(
        'HtmlEntities'
      ),
    ));
    $this->toValues->getValidator('NotEmpty')->setMessage($translate->translate("Please type a friend name."));
    Engine_Form::addDefaultDecorators($this->toValues);
    
    $this->addElement('Textarea', 'body', array(
      //'required' => true,
      //'allowEmpty' => false,
      'filters' => array(
        new Engine_Filter_HtmlSpecialChars(),
        new Engine_Filter_EnableLinks(),
        new Engine_Filter_Censor(),
      ),
    ));

    // Buttons
    $buttons = array();

    // Facebook
    $facebookTable = Engine_Api::_()->getDbtable('facebook', 'user');
    if( 'publish' == Engine_Api::_()->getApi('settings', 'core')->core_facebook_enable &&
        $facebookTable->getApi() &&
        $facebookTable->isConnected() ) {
      $this->addElement('Dummy', 'post_to_facebook', array(
        'content' => '
          <span href="javascript:void(0);" id="composer_facebook_toggle" class="composer_facebook_toggle" onclick="toggleFacebookShareCheckbox();">
            <span class="composer_facebook_tooltip">
              ' . $translate->translate('Publish this on Facebook') . '
            </span>
            <input type="checkbox" name="post_to_facebook" value="1" style="display:none;">
          </span>',
      ));
      $this->getElement('post_to_facebook')->clearDecorators();
      $buttons[] = 'post_to_facebook';
    }

    // Twitter
    $twitterTable = Engine_Api::_()->getDbtable('twitter', 'user');
    if( 'publish' == Engine_Api::_()->getApi('settings', 'core')->core_twitter_enable &&
        $twitterTable->getApi() &&
        $twitterTable->isConnected() ) {
      $this->addElement('Dummy', 'post_to_twitter', array(
        'content' => '
          <span href="javascript:void(0);" id="composer_twitter_toggle" class="composer_twitter_toggle" onclick="toggleTwitterShareCheckbox();">
            <span class="composer_twitter_tooltip">
              ' . $translate->translate('Publish this on Twitter') . '
            </span>
            <input type="checkbox" name="post_to_twitter" value="1" style="display:none;">
          </span>',
      ));
      $this->getElement('post_to_twitter')->clearDecorators();
      $buttons[] = 'post_to_twitter';
    }


    $this->addElement('Button', 'submit', array(
      'label' => $translate->translate('Share'),
      'type' => 'submit',
      'ignore' => true,
      'decorators' => array('ViewHelper')
    ));
    $buttons[] = 'submit';

    $this->addElement('Cancel', 'cancel', array(
      'label' => $translate->translate('cancel'),
      'link' => true,
      'prependText' => ' '.$translate->translate('or').' ',
      'href' => '',
      'onclick' => 'parent.Smoothbox.close();',
      'decorators' => array(
        'ViewHelper'
      )
    ));
    
    $buttons[] = 'cancel';

    $this->addElement('Hidden', 'privacy', array(
        'id'      => 'privacy_set',
        'value'   => 0
    ));
    $buttons[] = 'privacy';

    $this->addDisplayGroup($buttons, 'buttons');
    $button_group = $this->getDisplayGroup('buttons');
  }
}